package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.UserInfo;
import util.DBAccess;

public class UserDao {
	

	// 通过用户姓名查询用户信息
		public UserInfo get(String username) {
			UserInfo user = new UserInfo();
			String sql = "select * from userinfo where username = ?";
			try (Connection conn = new DBAccess().getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
				ps.setString(1, username);
				ResultSet rs = ps.executeQuery();
				if (rs.next()) {
					user.setUserId(rs.getInt(1));
					user.setUserName(username);
					user.setGender(rs.getString(4));
					user.setUserPhone(rs.getString(5));
					user.setHobbies(rs.getString(6));
					user.setBirthday(rs.getString(7));
					user.setDescription(rs.getString(8));
					user.setImgUrl(rs.getString(9));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return user;
		}

	
	//注册功能
	public void add(UserInfo user) {
		String sql = "insert into userinfo values(null,?,?,?,?,null,null,null,null)";
		try (Connection conn = new DBAccess().getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
			ps.setString(1, user.getPassword());
			ps.setString(2, user.getUserName());
			ps.setString(3, user.getGender());
			ps.setString(4, user.getUserPhone());
			ps.execute();

			ps.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	//用户资料表完善用户信息
	public void update(String hobbies,String birthday,String description,String username) {
		String sql = "update userinfo set hobbies = ?, birthday = ?, description = ?where username = ?";
		try (Connection conn = new DBAccess().getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
			ps.setString(1, hobbies);
			ps.setString(2, birthday);
			ps.setString(3, description);
			ps.setString(4, username);
			
			ps.execute();
			ps.close();
			conn.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	
	// 通过车牌或用户名查询用户信息
	public UserInfo query(String value) {
		UserInfo user = new UserInfo();
		String sql = "select * from carinfo where carcard = ? or username = ?";
		try (Connection conn = new DBAccess().getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {

			ps.setString(1, value);
			ps.setString(2, value);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {

				String username = rs.getString(2);

				user = new UserDao().get(username);
			}
			ps.close();
			conn.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}
	
	//通过车主名获取车主头像
	public String getImgUrl(String username) {
		String imgPath = "";
		String sql = "select imgurl from userinfo where username = ?";
		try (Connection conn = new DBAccess().getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
			ps.setString(1, username);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				imgPath = rs.getString(6);
			}
			ps.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return imgPath;
	}

}
